import {
	authorTodo
} from '@/common/utils.js'
export default {
	methods: {
		async getSharePhoto() {
			this.shareCardAdd();
			this.$refs.loading.show();
			let {
				attrs,
				canvas,
				ctx
			} = this;
			const info = await this.getImageInfo(attrs.imageUrl);
			if (info) {
				let image = attrs.imageUrl;
				// #ifdef MP-WEIXIN
				if (this.onlyShare) {
					ctx = uni.createCanvasContext('cardCanvas', this);
				}
				// #endif
				ctx.clearRect(0, 0, canvas.width, canvas.height); //清空之前
				ctx.save();
				const wr = info.width / 250;
				const hr = info.height / 200;
				let width = 0,
					height = 0;
				if (wr > hr) {
					width = info.width / hr;
					height = 200;
				} else {
					width = 250;
					height = info.height / wr;
				}
				ctx.drawImage(info.path, 0, 0, info.width, info.height, 0, 0, width, height);
				ctx.restore();
				return new Promise(resolve => {
					ctx.draw(true, () => {
						uni.canvasToTempFilePath({
								x: (width - 250) / 2,
								y: (height - 200) / 2,
								width: 250, // 画布的宽
								height: 200, // 画布的高
								canvasId: 'cardCanvas',
								quality: 0.6,
								fileType: 'jpg',
								success: async r => {
									const url = await this.base64ToUrl(r.tempFilePath);
									this.$refs.loading.close();
									resolve(url);
								},
								fail(res) {
									resolve();
								}
							},
							this
						);
					});
				});
			} else {
				this.$refs.loading.close();
				return Promise.resolve(attrs.imageUrl);
			}
		},
		getImageInfo(url) {
			return new Promise(resolve => {
				uni.getImageInfo({
					src: url,
					success(res) {
						const {
							path,
							width,
							height
						} = res;
						resolve({
							path: path,
							width,
							height
						});
					},
					fail(res) {
						resolve({});
					}
				});
			});
		},
		drawDate(top) {
			const {
				ctx,
				attrs
			} = this;
			top += attrs.innerTB + 60; //60: 上边距
			const date = attrs.date;
			ctx.save();
			ctx.setFontSize(48);
			this.drawText({
				text: date[0],
				left: attrs.innerLR,
				top: top,
				lineHeight: 44
			});
			ctx.restore();
			ctx.save();
			ctx.setFontSize(22);
			this.drawText({
				text: date[1],
				left: attrs.innerLR + 68,
				top: top,
				lineHeight: 44
			});
			ctx.restore();
			ctx.save();
			ctx.setFontSize(22);
			this.drawText({
				text: '.',
				left: attrs.innerLR + 110,
				top: top,
				lineHeight: 44
			});
			ctx.restore();
			ctx.save();
			ctx.setFontSize(22);
			this.drawText({
				text: date[2],
				left: attrs.innerLR + 120,
				top: top,
				lineHeight: 44
			});
			ctx.restore();
			return top; //44:行高
		},
		saveToImage() {
			const {
				canvas
			} = this;
			// 把画布转化成临时文件
			uni.canvasToTempFilePath({
					x: 0,
					y: 0,
					width: canvas.width, // 画布的宽
					height: canvas.height, // 画布的高
					canvasId: 'cardCanvas',
					fileType: 'png',
					success: res => {
						this.tempFilePath = res.tempFilePath;
						this.$refs.loading.hide();
						this.shareButton = false;
					},
					fail: res => {
						this.$refs.loading.hide();
						uni.showToast({
							title: '保存失败，稍后再试',
							duration: 2000,
							icon: 'none'
						});
					}
				},
				this
			);
		},
		downloadSharePhoto() {
			this.shareCardAdd();
			authorTodo({
				type: 'writePhotosAlbum',
				reject: () => {
					this.$refs.loading.hide();
				},
				success: () => {
					// 保存图片至相册
					this.$refs.loading.show('保存中');
					uni.saveImageToPhotosAlbum({
						filePath: this.tempFilePath,
						success: res => {
							this.$refs.loading.hide();
							this.showShare = false;
							uni.showToast({
								title: '保存成功，可以去分享啦~',
								duration: 2000,
								icon: 'none'
							});
							this.$emit('close');
						},
						fail: () => {
							console.log('保存失败，稍后再试');
							this.$refs.loading.hide();
						}
					});
				}
			})
		}
	}
}
